package com.bob.demo.api.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bob.demo.api.model.Result;
import com.bob.demo.system.dao.MenuRepository;
import com.bob.demo.system.dao.RoleMenuRepository;
import com.bob.demo.system.dao.UserMapper;
import com.bob.demo.system.dao.UserRepository;
import com.bob.demo.system.model.User;
import com.bob.demo.system.service.IUserService;
import com.bob.demo.utils.MD5Utils;
import com.bob.demo.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("api")
@Api(value = "API模块")
public class ApiController {


	@Autowired
    private UserRepository userRepository;
	
	@Autowired
    private RoleMenuRepository roleMenuRepository;
	
	@Autowired
    private MenuRepository menuRepository;
	
	@Autowired
    private IUserService userService;
	
    @Autowired  
    private UserMapper userMapper;  
    
	@Value("${test.msg}")
	private String msg;
	
	@ApiIgnore//使用该注解忽略这个API
	@RequestMapping("/hi")
	public String home() {
		System.err.println("Hello World!");
		return "Hello World!";
	}
	@ApiIgnore//使用该注解忽略这个API
	@GetMapping("/helloworld")
	public String helloworld() {
		System.err.println("Hello World!2" + msg);
		return "helloworld";
	}
	@ApiIgnore//使用该注解忽略这个API
	@GetMapping("/helloworld2")
	public String helloworld2() {
		System.err.println("Hello World!2");
		return "helloworld";
	}

	@ApiIgnore//使用该注解忽略这个API
	@RequestMapping("/test")
	public String test() {
		
		String[] usernameArray = {"Mary", "Patricia", "Linda", "Barbara", "Elizabeth", "Jennifer", "Maria", "Susan","Margaret", "Dorothy", "Lisa", "Nancy", "Karen", "Betty", "Helen", "Sandra", "Donna","Carol", "Ruth", "Sharon", "Michelle", "Laura", "Sarah", "Kimberly", "Deborah", "Jessica","Shirley", "Cynthia", "Angela", "Melissa", "Brenda", "Amy", "Anna", "Rebecca", "Virginia","Beth", "Jeanne"};
		for(int i = 1;i<100;i++) {
			User user = new User();
			int num= (int)(Math.random()*usernameArray.length); 
			String username = usernameArray[num];
			String pswd ="111111";
			user.setUsername(username);
			user.setNickname(username);
			user.setPassword(MD5Utils.encrypt(username, pswd));
			user.setBirthday(new Date());
			userService.save(user);	
		}

		return "helloworld";
	}

	
	@ApiOperation(value="分页获取用户列表", notes="分页获取用户列表")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "页码", required = true, paramType = "query",dataType = "String"),
        @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true,paramType = "query", dataType = "String"),
	})
	@RequestMapping(value= "/userList", method= {RequestMethod.POST,RequestMethod.GET})
	public Result<Object> userList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		
		
		String pageNoStr = request.getParameter("pageNo");
		String pageSizeStr= request.getParameter("pageSize");
		int pageNo = 1;
		int pageSize = 10;
		if(!StringUtils.isNullOrEmpty(pageNoStr)) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		if(!StringUtils.isNullOrEmpty(pageSizeStr)) {
			pageSize = Integer.parseInt(pageSizeStr);
		}
//		Sort sort = new Sort(Direction.DESC, "createTime");
//		Pageable pageable = new PageRequest(pageNo, pageSize, sort);
//		Page<User> userList = userRepository.findAll(pageable);
		
		List<User> list = userMapper.getAll();
		PageHelper.startPage(1, 2);
	    PageInfo<User> page=new PageInfo<User>(list);
		Result<Object> result = new Result<Object>("200", "获取数据成功", true, page);
		
		return result;
	}
	
	@RequestMapping(value = "/getUserByName", method= {RequestMethod.POST,RequestMethod.GET})
	@ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "name", required = true, paramType = "query", dataType = "String")
	})
	public Result<Object> getUserByName(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		
		
		String name = request.getParameter("name");
		List<User> userList = userRepository.findUserByName(name);
		System.err.println(userList.size());
		Result<Object> result = new Result<Object>("200", "获取数据成功", true, userList);
		return result;
	}
	
	@RequestMapping(value = "/getUserById" , method= {RequestMethod.POST,RequestMethod.GET})
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
	})
	public Result<Object> getUserById(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		
		
		String id = request.getParameter("id");
		User user = userRepository.findUserById(id);
		Result<Object> result = new Result<Object>("200", "获取数据成功", true, user);
		return result;
	}
	
	@RequestMapping(value = "/deleteUserById", method= {RequestMethod.POST,RequestMethod.GET})
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
	})
	public Result<Object> deleteUserById(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		
		
		String id = request.getParameter("id");
		System.err.println("id:"+id);
		userRepository.deleteByUserId(id);
		Result<Object> result = new Result<Object>("200", "删除成功", true);
		return result;
	}
	
	@RequestMapping(value = "/findMenuIdList" , method= {RequestMethod.POST,RequestMethod.GET})
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
	})
	public Result<Object> findMenuIdList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		
		//获取所有角色的所有菜单
		String userId = "1";
//		List<String> menuIdList = roleMenuRepository.findMenuIdList(roleIdS);//用户的所有角色
//		System.err.println(menuIdList.size());
//		List<Menu> m =menuRepository.findMenuByUserId("1");
		Result<Object> result = new Result<Object>("200", "获取数据成功", true, userService.listMenuTree(userId));
		return result;
	}
	
}
